package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lgqz.himalayafm.DrawListener;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.RadioStationAdapter;
import com.lgqz.himalayafm.bean.RadioHomeStationEntity;
import com.lgqz.himalayafm.http.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RadioFragmentSlidingListView extends Fragment implements Callback<RadioHomeStationEntity>, View.OnClickListener {


    private View view;
    private ListView mRadioSkipListview;
    private Button mStationBack;
    private List<RadioHomeStationEntity.HomeStationEntityData> homeStationData;
    private RadioHomeStationEntity homeStstion;
    private RadioStationAdapter adapter;
    private  String station;
    private TextView mTextStation;
    private DrawListener listener;


    public RadioFragmentSlidingListView() {


    }

    public static RadioFragmentSlidingListView newInstance(String station) {

        Bundle args = new Bundle();
        args.putString("station",station);

        RadioFragmentSlidingListView fragment = new RadioFragmentSlidingListView();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle args = getArguments();
        listener= (DrawListener) getActivity();

        station = args.getString("station");



        view = inflater.inflate(R.layout.fragment_radio_fragment_sliding_list_view, container, false);
         //获得控件的实例
        mRadioSkipListview = (ListView) view.findViewById(R.id.radio_skip_listview);
        mStationBack = ((Button) view.findViewById(R.id.home_station_back));
        mTextStation = ((TextView) view.findViewById(R.id.radio_change_back));
        //开始下载数据
        //给返回按钮设置一个监听
        mStationBack.setOnClickListener(this);

        switch (station) {
            case "本地":
                mTextStation.setText("本地台");
                downloadData();
                break;
            case "国家":
                mTextStation.setText("国家台");
                downCounStaData();
                break;
            case "网络":
                mTextStation.setText("网络台");
                downNetStaData();
                break;
        }
        //数据源的准备
        homeStationData = new ArrayList<>();
      //适配器
        adapter = new RadioStationAdapter(getContext(),homeStationData);
        mRadioSkipListview.setAdapter(adapter);
        return view;

    }
    //开始下载本地台数据
    private void downloadData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getHomStaData(1,20,"370000","2").enqueue(this);

    }

    /**
     * 开始下载国家台数据
     */
    private void downCounStaData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getCounStaData(1,20,"1").enqueue(this);

    }

    /**
     * 开始下载网络台数据
     */
    private void downNetStaData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getNetStaData(1,20,"3").enqueue(this);

    }
    @Override
    public void onResponse(Call<RadioHomeStationEntity> call, Response<RadioHomeStationEntity> response) {
        homeStstion = response.body();
        homeStationData.addAll(homeStstion.getResult());
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onFailure(Call<RadioHomeStationEntity> call, Throwable t) {

    }

    /**
     * 点击返回按钮回到原始界面
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_station_back:
                listener.closeDraw();


        }
    }
}
